/*
* DS130X
* Author : Vo Tuan Anh
* Email  : votuananhs@gmail.com
*/

#include "stdint.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_i2c.h"
#include "i2c.h"

#ifndef _DS130X_H_
#define _DS130X_H_

#ifdef __cplusplus
 extern "C" {
#endif /* __cplusplus */
#define ADDRESS_DEVICE_DS1302   ((uint8_t)0xD0)
#define LENGTH_DATA             ((uint8_t)0x07)
   
/* MASK Regsister Address */
#define MASK_WRITE_ADDRESS      ((uint8_t)0x00)  
#define MASK_READ_ADDRESS       ((uint8_t)0x01)

#ifdef _DS1302_
/* TIMER */
#define ADDRESS_SEC             ((uint8_t)0x80)
#define ADDRESS_MIN             ((uint8_t)0x82)
#define ADDRESS_HR              ((uint8_t)0x84)
#define ADDRESS_DATE            ((uint8_t)0x86)
#define ADDRESS_MONTH           ((uint8_t)0x88)
#define ADDRESS_DAY             ((uint8_t)0x8A)
#define ADDRESS_YEAR            ((uint8_t)0x8C)   
   
#define MASK_SEC                ((uint8_t)0x7F)
#define MASK_MIN                ((uint8_t)0x7F)
#define MASK_HR_12              ((uint8_t)0x1F)
#define MASK_HR_24              ((uint8_t)0x3F)
#define MASK_DATE               ((uint8_t)0x3F)
#define MASK_MONTH              ((uint8_t)0x1F)
#define MASK_DAY                ((uint8_t)0x07)
#define MASK_YEAR               ((uint8_t)0xFF)
#define MASK_AM_PM              ((uint8_t)0x20)
#define MASK_12_24              ((uint8_t)0x80)

/* CONTROL DS1302 */
#define MASK_CONTROL_ADDRESS            ((uint8_t)0x8E)
#define MASK_TRICKLE_CHARGER_ADDRESS    ((uint8_t)0x90)
#define MASK_CLOCK_BURST_ADDRESS        ((uint8_t)0xBE)

#define MASK_RAM_ADDRESS        ((uint8_t)0xC0)
#endif /* _DS1307_ */

/* DS1307 */
#ifdef _DS1307_
/* TIMER */
#define ADDRESS_SEC             ((uint8_t)0x00)
#define ADDRESS_MIN             ((uint8_t)0x01)
#define ADDRESS_HR              ((uint8_t)0x02)
#define ADDRESS_DATE            ((uint8_t)0x03)
#define ADDRESS_MONTH           ((uint8_t)0x04)
#define ADDRESS_DAY             ((uint8_t)0x05)
#define ADDRESS_YEAR            ((uint8_t)0x06)   
   
#define MASK_SEC                ((uint8_t)0x7F)
#define MASK_MIN                ((uint8_t)0x7F)
#define MASK_HR_12              ((uint8_t)0x1F)
#define MASK_HR_24              ((uint8_t)0x3F)
#define MASK_DATE               ((uint8_t)0x3F)
#define MASK_MONTH              ((uint8_t)0x1F)
#define MASK_DAY                ((uint8_t)0x07)
#define MASK_YEAR               ((uint8_t)0xFF)
#define MASK_AM_PM              ((uint8_t)0x20)
#define MASK_12_24              ((uint8_t)0x80)

/* CONTROL DS1307 */
#define MASK_CONTROL_ADDRESS            ((uint8_t)0x07)

#define MASK_RAM_ADDRESS        ((uint8_t)0xC0)
#endif /* _DS1307_ */

/* Channel I2C */
#define CHANNEL_I2Cx            I2C1

#define RES_DS1302              ((uint16_t)GPIO_Pin_5)
#define RES_CLK                 RCC_AHB1Periph_GPIOB
#define RES_PORT                GPIOB

/* struct time */
typedef struct
{
  uint8_t Sec; /* seconds */
  uint8_t Min; /* Minutes */
  uint8_t Hour; /* Hours */
  uint8_t Day; /* Day of week */
  uint8_t Date; /* Day of month */
  uint8_t Month; /* Month */
  uint8_t Year; /* Month */
  uint8_t PM_AM; /* select PM/AM */
  uint8_t M12_24; /* select PM/AM */
} typeTime;


/* function global */
void Init_DS130X(void);
void WriteTime_DS130X(typeTime *inputTime);
void ReadTime_DS130X(typeTime *inputTime);


#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _DS130X_H_ */
